package and.activity;

import and.common.MyCommon;
import and.process.QueryProcess;
import and.process.StatisticsProcess;
import and.tools.ViewHelper;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class StatisticsActivity extends BaseActivity {
	private ImageButton btnBack;
	private TextView tvNum;
	private TextView tvTotal;
	private TextView tvDivider;
	private TextView tvText;
	private StatisticsProcess statisticsProcess =new StatisticsProcess();
	private ProgressDialog progressDialog;
	private String result;
	private Handler handler = new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		statistics();
		super.onCreate(savedInstanceState);
		setNowActivity(this);

	}

	private void statistics(){
		progressDialog = ProgressDialog.show(this.getParent(),MyCommon.STATISTICSACTION+MyCommon.QueryName,"统计数据中……",true);

		new Thread(new Runnable() {
			public void run() {
				try{
					QueryProcess query = new QueryProcess();
					if(query.hasNewData()){
						query.query();
					}
					result = statisticsProcess.getMostNum();
				}catch (Exception e) {
					e.printStackTrace();
				}
				setContentByHandler();
				progressDialog.dismiss();
			}
		}).start();
	}

	private void setMyContent(){
		ScrollView sv =(ScrollView)this.getLayoutInflater().inflate(R.layout.scrollview, null);
		LinearLayout ll = (LinearLayout)sv.getChildAt(0);
		btnBack = new ImageButton(this);
		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		LayoutParams paramsWeight = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1f);
		LayoutParams paramsNum = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1f);
		paramsNum.width =100;
		paramsNum.height =60;
		paramsNum.topMargin =10;
		paramsNum.bottomMargin =10;
		paramsNum.leftMargin = 5;
		paramsWeight.gravity = Gravity.CENTER;
		RelativeLayout.LayoutParams paramsBtn = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		paramsBtn.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		paramsBtn.width = 80;
		paramsBtn.height = 80;

		String[] datas = result.split("\n");
		RelativeLayout  headLl = new RelativeLayout(this);
		headLl.setLayoutParams(paramsWeight);

		tvTotal =new TextView(this);
		tvTotal.setLayoutParams(params);

		btnBack.setLayoutParams(paramsBtn);
		btnBack.setBackgroundResource(R.drawable.back);
		btnBack.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				try{
					btnBack.setBackgroundResource(R.drawable.gray);

					BaseActivityGroup.changeActivity(StatisticsActivity.this,"StatisticsListActivity");
				}catch (Exception e) {
					System.out.println("返回出错");
					e.printStackTrace();
				}
			}
		});

		LinearLayout ll1 = new LinearLayout(this);
		ll1.setLayoutParams(params);
		ll1.setOrientation(LinearLayout.HORIZONTAL);

		tvTotal.setText(MyCommon.QueryName+"\n共统计"+MyCommon.QishuCount+"期的数据");
		tvTotal.setTextSize(26);
		ll.addView(headLl);
		headLl.addView(tvTotal);
		headLl.addView(btnBack);
		int times = 0;
		float rate =0;
		for(int i=0;i<datas.length;i++){
			ll1 = new LinearLayout(this);
			tvNum = new TextView(this);
			tvText = new TextView(this);
			tvDivider =ViewHelper.getDivider(this, 1);
			tvNum.setLayoutParams(paramsNum);
			tvNum.setTextSize(22);
			tvNum.setGravity(Gravity.CENTER);
			tvNum.setBackgroundResource(R.drawable.ball_red_32x32);
			tvNum.setText(datas[i].split("=")[0]);

			tvText.setLayoutParams(paramsWeight);
			tvText.setTextSize(22);
			tvText.setPadding(10,0,0,0);
			times=Integer.parseInt(datas[i].split("=")[1]);
			rate =((float)times/MyCommon.QishuCount)*100;
			rate =  (float)(Math.round(rate*100))/100;
			tvText.setText("出现次数："+times+"\n出现几率："+rate+"%");
			ll.addView(tvDivider);
			ll.addView(ll1);

			ll1.addView(tvNum);
			ll1.addView(tvText);
		}

		setContentView(sv);
	}

	private void setContentByHandler(){
		handler.post(new Runnable() {
    		public void run() {
    			//将获取到的数据更新到列表中
    			setMyContent();
    		}
    	});
	}
}
